package com.sgb.kjwl.viewmodel.machine;

import android.util.Log;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.lease.MyAskLeaseQutedEntity;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.leaseAsk.MyAskLeaseQutedListActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyAskLeaseQutedListModel extends BaseViewModel {

    private MyAskLeaseQutedListActivity askLeaseActivity;
    private MachineRepertory repertory;
    private int pageSize = 10;

    public MyAskLeaseQutedListModel(MyAskLeaseQutedListActivity askLeaseActivity, MachineRepertory repertory) {
        this.askLeaseActivity = askLeaseActivity;
        this.repertory = repertory;
    }

    //查看报价0机械 1设备 2 车辆，3房产 4土地 5工厂
    public void getData(String id,String categoryType, int pageNum) {
        APIManager.getInstance().getMachineAPI().getLeaseQutedList(pageSize, pageNum,id,categoryType).enqueue(new Callback<BaseEntity<ProListEntity<List<MyAskLeaseQutedEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<MyAskLeaseQutedEntity>>>> call, Response<BaseEntity<ProListEntity<List<MyAskLeaseQutedEntity>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData() && null != response.body().getData().getList()) {
                        askLeaseActivity.showView(response.body().getData().getList());
                    } else {
                        askLeaseActivity.showView(new ArrayList<>());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<MyAskLeaseQutedEntity>>>> call, Throwable t) {
                Log.v("dd", "sdsdd");
            }
        });
    }

    public void getH5Url(int position, String id) {

        String urlName = "";
        if (position == 0) {
            urlName = "personCenterMechanicalDetail";
        } else if (position == 1) {
            urlName = "persenCenterEquipmentDetail";
        }

        DialogHelper.showProgressDialog(askLeaseActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
//                    String url = "http://192.168.2.49:8067/tenderNoticeDetail" + "?id=" + id;
                    WebViewX5HostActivity.start(askLeaseActivity, url);
                } else {
                    MToast.showToast(askLeaseActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
